package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import validacion.ValidadorDeRangoEntero;
import validacion.ValidadorStrNoVacio;
import validacion.ValidadoresCombinados;

import logica.Enfermero;

@SuppressWarnings("serial")
public class Ingreso_Datos_Enfermero extends JPanel {

	private JPanel p0, p1; // p1 va adentro de p0
	
	// Variables del panel 1
	private JTextField[] campos;
	private JLabel[]	 labels;
	private String[]     textos = {"ID Enfermero:"}; 
	private JComboBox    cap_combo;
	private String[]     combo_strings = {"C.A.P Rojas (id=1)", "C.A.P San Martin (id=2)"};
	
	private final int cant_campos_pac = textos.length;
	
	private ValidadoresCombinados valid_id;
	
	private BufferedImage image;
	
	/* Constructor */
	public Ingreso_Datos_Enfermero(){
		setLayout(new BorderLayout());		
		
		try {
			image = ImageIO.read(new File("img/mainbanner.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		p0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p0.setBorder(BorderFactory.createTitledBorder("Datos Enfermero"));
		
		// Creo panel 1 
		p1 = new JPanel(new GridLayout(cant_campos_pac, 2, 10, 0));
		
		campos = new JTextField[cant_campos_pac];
		labels = new JLabel[cant_campos_pac];
		
		for(int i=0;i<cant_campos_pac;i++){
			labels[i] = new JLabel(textos[i] + "*");
			labels[i].setPreferredSize(new Dimension(120, 25));
			labels[i].setSize(getPreferredSize());
			labels[i].setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			campos[i] = new JTextField();
			campos[i].setPreferredSize(new Dimension(100, 25));
			campos[i].setSize(getPreferredSize());
			campos[i].setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			p1.add(labels[i]);
			p1.add(campos[i]);
		}
	
		p0.add(p1);
		add(p0, BorderLayout.NORTH);
		
		// Agrego el combobox (dentro de un panel)
		JPanel combo_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		combo_panel.setBorder(BorderFactory.createTitledBorder("C.A.P Destino: "));
		
		cap_combo = new JComboBox(combo_strings); 
		cap_combo.setSelectedIndex(0);
		combo_panel.add(cap_combo);
		add(combo_panel, BorderLayout.CENTER);
		
		
		JPanel leyenda_panel = new JPanel(new FlowLayout());
		JLabel leyenda = new JLabel("Los campos marcados con * son obligatorios");
		leyenda_panel.add(leyenda);
		
		valid_id = new ValidadoresCombinados(
				   leyenda, 
				   new ValidadorStrNoVacio(),
			       new ValidadorDeRangoEntero(0, 1000, false, null));
		
		campos[0].setInputVerifier(valid_id);
		
		add(leyenda_panel, BorderLayout.SOUTH);
	}
	
	public Enfermero getEnfermero(){
		int id; 
		
		id = Integer.valueOf(campos[0].getText()); // NumberFormatException
		Enfermero emf = new Enfermero();
		emf.setId(id);
		
		return emf;
	}
	
	public int getIDdestino(){
		return cap_combo.getSelectedIndex() + 1;
	}

	public boolean son_todos_los_datos_validos() {
		return valid_id.sonLosDatosValidos();
	}

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 100, null); // see javadoc for more info on the parameters            
    }
}
